import axios from 'axios';
import qs from 'qs';
import {instance0,instance,instance2,instance3} from './request'

const API_DOMAIN = 'https://service-c2zjvuxa-1252463788.gz.apigw.tencentcs.com';
const CancelToken = axios.CancelToken;
export let cancel;
export let cancel0;
// 获取token
export const getToken = data => {
  return instance0({
    url: '/oauth/2.0/token?client_id=' + data.AK + '&client_secret=' + data.SK+'&grant_type=client_credentials',
    method: 'post',
  })
}
// 创建语音合成任务
export const createTaskid = (data) => {
  return instance0({
    url: '/rpc/2.0/tts/v1/create?access_token=' + data.token,
    method: 'post',
    data: data,
    cancelToken: new CancelToken(function executor(c) {
      // executor 函数接收一个 cancel 函数作为参数
      cancel0 = c;
    })
  })
}
// 查询语音合成
export const getAudioUrl = (data) => {
  return instance0({
    url: '/rpc/2.0/tts/v1/query?access_token=' + data.token,
    method: 'post',
    data: data,
    cancelToken: new CancelToken(function executor(c) {
      // executor 函数接收一个 cancel 函数作为参数
      cancel = c;
    })
  })
}
//获取视频参数
export const getVideoParams = data => {
  return instance({
    url: '/api/v1/resident/terminal/videoparam?resID='+data,
    method: 'get',
  })
}
//设置视频参数
export const setVideoParams = data => {
  return instance({
    url: '/api/v1/resident/terminal/videoparam?resID='+data.userID,
    method: 'post',
    data: data
  })
}
//生成智能终端影像记录
export const terminalCreate = data => {
  return instance({
    url: '/api/v1/resident/terminal/create?resID='+data.userID,
    method: 'post',
    data: data
  })
}
//trtc文字转语音
export const trtcSpeech = data => {
  return instance2({
    url: '/api/v1/speech/synthesise',
    method: 'post',
    data: data
  })
}
// 一句话识别
export const recognise = data => {
  return instance2({
    url: '/api/v1/speech/recognise',
    method: 'post',
    data: data
  })
}
// 闲聊
export const conversate = data => {
  //trtc 闲聊接口
  // return instance2({
  //   url: '/api/v1/speech/conversate',
  //   method: 'post',
  //   data: data
  // })
  //百度 文心一言
  return instance3({
    url: '/api/v1/Chat/chat/inquire/create?inquireMessage='+data,
    // url: '/chat/inquire/create?inquireMessage='+data,
    method: 'post',
  })
}
//移除trtc用户
export const trtcRemoveUser = data => {
  return instance2({
    url: '/v1/room/bystr/removeuser',
    method: 'post',
    data: data
  })
}
//trtc查询房间是否有人
export const getTrtcRoomUser = data => {
  return instance2({
    url: '/api/v2/room/records?PageNumber=0&PageSize=100&rooms='+ JSON.stringify(data),
    method: 'get',
    data: data
  })
}
//登录
export const getTrtcLogin = data => {
  return instance2({
    url: '/api/auth/login?acount=admin_nikoyose_3033654362&paswd=admin_nikoyose_3033654362&appid=1400552732&api-version=1',
    method: 'post',
    data: data
  })
}
//生理数据-心率图
export const getPhysiologicalHeart = data1 => {
  let data = JSON.parse(data1)
  return instance({
    url: '/api/v1/medical/physiological/'+data.type+'?resid='+data.resid+'&date1='+data.date1+'&date2='+data.date2,
    method: 'get',
  })
}
//获取生理数据
export const getPhysiological = data => {
  return instance({
    url: '/api/v1/medical/physiological?resid='+data,
    method: 'get',
  })
}
//刷新token
export const getNewtoken = data => {
  return instance({
    url: '/api/auth/RefreshToken',
    method: 'post',
    data: data
  })
}
//登录
export const getUserInfoDetails = data => {
  return instance({
    url: '/api/auth/login',
    method: 'post',
    data: data
  })
}
//心跳
export const getActives = data => {
  return instance({
    url: '/api/auth/selfhealth?tyid='+data,
    method: 'get',
  })
}
// 睡眠统计-日报
export const GetDailyReport = (userid) => {
  return instance({
    url: "/api/v1/report/today?resid=" + userid,
    method: 'get',
  })
}
// 睡眠统计日报（以往）
export const GetDailyReportAgo = (data1) => {
  let data = JSON.parse(data1)
  return instance({
    url: "/api/v1/report/day?rsid=" + data.userid + "&date=" + data.date,
    method: 'get',
  })
} 
export const GetSleepDeepInfo = (data1) => {
  let data = JSON.parse(data1)
  return instance({
    url: "/api/v1/sleepdeep/info?rsid=" + data.userid + "&date=" + data.date,
    method: 'get',
  })
}
// 睡眠统计-周报
export const GetWeekReport = (data1) => {
  let data = JSON.parse(data1)
  return instance({
    url: "/api/v2/report/week?rsid=" + data.userid + "&date=" + data.date,
    method: 'get',
  })
}
// 睡眠统计-月报
export const GetMonthreport = (data1) => {
  let data = JSON.parse(data1)
  return instance({
    url: "/api/v2/report/month?rsid=" + data.userid + "&date=" + data.date,
    method: 'get',
  })
}
// //获取栅栏信息
// export const getFence = data => {
//   return instance({
//     url: '/api/v1/fence/info/'+data,
//     method: 'get',
//   })
// }

export async function getSmsVerifyCode(phoneNum) {
  let data = {
    method: 'getSms',
    phone: phoneNum
  };
  const options = buildOptions(data, '/release/sms')
  return axios(options);
}

export async function loginSystemByVerifyCode(loginInfo) {
  const {phoneNum, sessionId, verifyCode} = loginInfo;
  let data = {
    method: 'login',
    phone: phoneNum,
    code: verifyCode,
    sessionId
  };
  const options = buildOptions(data, '/release/sms');
  return axios(options);
}

export async function loginSystemByToken(phoneNum, token) {
  let data = {
    method: 'login',
    phone: phoneNum,
    token
  };
  const options = buildOptions(data, '/release/sms');
  return axios(options);
}

export async function fetchUserInfoByPhone(phone, token) {
  let data = {
    phone,
    token
  };
  const options = buildOptions(data, '/release/getUserInfo');
  return axios(options);
}

export async function fetchUserInfoByUserId(userId, token) {
  let data = {
    userId,
    token
  };
  const options = buildOptions(data, '/release/getUserInfo');
  return axios(options);
}

export async function updateUserName(name, userId, token) {
  let data = {
    userId,
    name,
    token
  };
  const options = buildOptions(data, '/release/setNickname');
  return axios(options);
}

function buildOptions(data, apiPath) {
  return {
    method: 'POST',
    headers: { 'content-type': 'application/x-www-form-urlencoded' },
    data: qs.stringify(data),
    url: `${API_DOMAIN}${apiPath}`,
  }
}
// 写入聊天记录
export const  chatMessage = data=> {
  return instance({
    url: '/api/v1/Chat/message/create',
    method: 'post',
    data: data
  })
}
//  聊天记录查询
export const  chatRecoeds = data=> {
  return instance({
    url: `/api/v1/Chat/message/records?isUserID=${data.isUserID}&userID=${data.userID}&isDate=${data.isDate}&date=${data.date}&date2=${data.date2}&pageSize=10000&pageIndex=1`,
    method: 'get',
  })
}
// ５．２．８５查询智能终端应用-权限参数
export const  getPermissions = (data) => {
  return instance({
    url: `/api/v1/resident/terminal/service/permissions/detail/${data.accountName}`,
    method: 'get',
  })
}
// ５．２．８８设置住户自主对话控制参数
export const  setChatParams = data=> {
  return instance({
    url: `/api/v1/resident/terminal/chat/gpt/param/set?resID=${data.resID }`,
    method: 'post',
    data: data.data
  })
}
// ５．２．８９查询住户自主对话控制参数
export const  getChatParams = (data) => {
  return instance({
    url: `/api/v1/resident/terminal/chat/gpt/param/detail?resID=${data.resID}`,
    method: 'get',
  })
}

// 住户夜间护理记录（根据住户id获取睡眠传感器id）
export const getNightCareInfo = (userid) => {
  return instance({
    url: `api/v1/supervise/info?resid=${userid}`,
    method: 'get',
  })
}
